package com.jluzh10.learning.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jluzh10.learning.domain.Answer;
import com.jluzh10.learning.domain.Question;
import com.jluzh10.learning.domain.User;
import com.jluzh10.learning.service.QuestionService;
import com.jluzh10.learning.utils.ResponseData;
import com.jluzh10.learning.web.converter.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cj
 * @date 5/25/2019
 */
@Controller
@RequestMapping("question")
@ResponseBody
public class QuestionController {

    private final QuestionService qs;

    List<Question> questionList = new ArrayList<>();//定义为全局变量，(若为局部变量每次执行都会声明一个引用)

    @Autowired
    public QuestionController(QuestionService qs) {
        this.qs = qs;
    }


    @RequestMapping(value = "add/{courseId}", method = RequestMethod.GET)
    public ModelAndView toQuestion(@PathVariable(value = "courseId") Integer courseId, ModelAndView modelAndView) {
        modelAndView.addObject("courseId",courseId);
        modelAndView.setViewName("question");
        return modelAndView;
    }

    /**
     * 新增一个问题
     *
     * @param question
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseData addQu(Question question, @LoginUser User user) {
        if (user != null) {
            System.out.println(question);
            question.setUserId(user.getId());
            question.setCreateTime(new Date());
            int i = qs.addQu(question);//插入问题
            if (i > 0) {//插入成功

                return new ResponseData().success().data(question);//插入后的question实体类已有主键id
            } else {
                return new ResponseData().fail("更新问题失败");//插入失败
            }
        } else {
            //跳转到登陆页面
            return new ResponseData().redirect("该用户尚未登陆");
        }

    }

    @RequestMapping(value = "/{questionId}",method = RequestMethod.POST)
    public ResponseData questionJson(@PathVariable(value = "questionId")Integer questionId){
        Question question = qs.searchQNUserService(questionId);//查询当前问题和对应的用户信息
        return new ResponseData().success().data(question);
    }


    /**
     * 查询某个问题并转发到answer.jsp
     */
    @RequestMapping(value = "/{questionId}",method = RequestMethod.GET)
    public ModelAndView questionSearch(@PathVariable(value = "questionId") Integer questionId, ModelAndView mv) {

        Question q = qs.searchQNUserService(questionId);//查询当前问题和对应的用户信息
        mv.setViewName("answer");
        mv.addObject("question", q);
        return mv;
    }


    /**
     * 根据课程id查询该课程下的所有问题和最新的回答
     *
     * @param question 查询对象
     * @param limit    每页显示条数
     * @param offset   页码
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseData searchAllQuestionBy(Question question,
                                            @RequestParam(name = "limit") int limit,
                                            @RequestParam(name = "offset") int offset, HttpSession session) {
        PageHelper.startPage(offset, limit);
        questionList = qs.searchAllQuestionService(question);

        User user = (User) session.getAttribute("user");

        if (questionList != null && !questionList.isEmpty()) {
            for (Question q : questionList) {
                if (user != null && (user.getId().equals(q.getUserId()))) { //判断当前问题的用户是否是当前已登录的用户
                    //若该问题是当前用户所提出，返回可删除图标
                    q.setDelFlag(1);
                }
                if (q.getAnswer()==null){//问题没有回答时
                    q.setAnswer(new Answer(0));
                }
            }
            PageInfo<Question> pageInfo = new PageInfo<>(questionList);
            return new ResponseData().success("根据课程id查询问题成功").data(pageInfo);
        } else {
            return new ResponseData().fail("查询结果为空");
        }

    }


}
